﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.SceneManagement;

namespace UGUIMan
{
    public class UGUIManBody:MonoBehaviour
    {
        public Arm armleft;
        public Arm armright;

        public GameObject Hitarma;
        public GameObject Fire;
        public GameObject SuperPowerAmra;
        public Rect feet;
        public Feets feets;
        public Collider2D col2dtouch;
        public Collider2D col2drotate;
        public Collider2D col2dhit;
        public float upForce;
        public Map map;
        public float energy;
        public Vector3 right;
        Collider2D[] r = new Collider2D[10];

        public ConstantForce2D cf2d;

        public void Awake()
        {
            Fire = transform.Find("Fire").gameObject;
            feets = GameObject.Find("Feets").GetComponent<Feets>();
            cf2d = GetComponent<ConstantForce2D>();
        }

        public void PressA()
        {
            energy += 0.01f;
            armleft.speedup();
        }

        public void PressD()
        {
            energy += 0.01f;
            armright.speedup();
        }

        public enum State
        {
            Null,Idle,Jump,Hit,SuperPower
        }
        public State state;

        private State m_state
        {
            get
            {
                return state;
            }
            set
            {
                state = value;
                Invoke(state.ToString(), 0);
            }
        }
        //bool JumpStart;
        public void Jump()
        {
            /*
            if (!Jumping)
            {
                float JumpTop =transform.position.y+JumpHeight;
                Jumping = true;
                grave = false;
            }*/
            Jumping = true;
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, upForce));
            /*
            StartCoroutine(JumpCon());
            Jumpdelta = Mathf.Lerp(transform.position.y, JumpTop,0.05f);
            if (JumpTop - Jumpdelta < 1)
            {
                transform.position = new Vector3(transform.position.x, JumpTop, transform.position.z);
                //Jumping = false;
                grave = true;
            }
            else
            {
                transform.position = new Vector3(transform.position.x, Jumpdelta, transform.position.z);
            }*/
        }

        bool grave
        {
            get
            {
                return GetComponent<Rigidbody2D>().gravityScale > 0;
            }
            set
            {
                if (value)
                {
                    GetComponent<Rigidbody2D>().gravityScale = 1;
                }
                else
                {
                    GetComponent<Rigidbody2D>().gravityScale = 0;
                }
            }
        }
        bool Jumping;
        float JumpTop;
        public float JumpHeight;
        float Jumpdelta;
        
        IEnumerator JumpCon()
        {
            yield return new WaitForSeconds(0.01f);
            if (!grave)
            {
                Jump();
            }
        }

        public void Null()
        {
            state = State.Idle;
        }

        public void Idle()
        {
            
        }

        public void Hit()
        {
            grave = true;
            Jumping = false;
        }

        void Energy()
        {

        }

        public void SuperPower()
        {
            SuperPowerAmra.GetComponent<SuperPower>().Show=true;
        }

        void PressW()
        {
            state = State.Jump;
            Jump();
        }

        private void OnCollisionEnter2D( Collision2D collision )
        {
            if (collision.gameObject.tag == "Enemy")
            {
                collision.gameObject.GetComponent<SuperPower>().Kill();
            }
            else if(collision.gameObject.tag=="map")
            {
                feets.Rotate(collision.gameObject);
            }
        }

        private void OnCollisionStay2D( Collision2D collision )
        {
            if (collision.gameObject.tag == "map")
            {
                state = State.Idle;
                JumpCheck = true;
            }
            else if(collision.gameObject.tag=="Enemy")
            {
                HP.current -= collision.gameObject.GetComponent<Enemy>().hit;
            }
        }


        bool JumpCheck = false;
        /* bool JumpCheck()
         {
             var feettouch = GameObject.Find("FeetTouch");
             ContactFilter2D cf2d = new ContactFilter2D();
             Vector2 lb = (
                 new Vector2(-feettouch.transform.right.x, -feettouch.transform.right.y) +
                 new Vector2(-feettouch.transform.up.x, -feettouch.transform.up.y)
                 ) / 2;
             Vector2 rb= (
                 new Vector2(feettouch.transform.right.x, feettouch.transform.right.y) +
                 new Vector2(-feettouch.transform.up.x, -feettouch.transform.up.y)
                 ) / 2;

             var posfeet = feettouch.transform.position;
             var lbtest = new Vector2(posfeet.x + lb.x * 10, posfeet.y + lb.y * 10);

             var rbtest = new Vector2(posfeet.x, posfeet.y) + rb * 10;

             RaycastHit2D rh2dlb = Physics2D.Raycast(lbtest,lbtest+lb);
             RaycastHit2D rh2drb = Physics2D.Raycast(rbtest,rbtest+rb);
             Debug.Log(lbtest);
             Debug.DrawLine(new Vector3(lbtest.x,lbtest.y,0),new Vector3(lbtest.x+10 * lb.x, lbtest.x + 10 * lb.y,0));
             if (rh2dlb != default(RaycastHit2D))
             {
                 if (rh2dlb.collider.tag == "map")
                 {
                     Debug.Log("mapCheck");
                 }
                 if (rh2dlb.distance < 10 && rh2dlb.collider.tag=="map")
                 {
                     return true;
                 }
             }
             return false;
         }
         */

        bool reloadflag;

        private void Update()
        {
            if (energy < 0)
            {
                energy = 0;
            }
            else
            {
                energy -= 0.01f;
                if (energy < 10)
                {
                    Fire.transform.GetChild(0).gameObject.SetActive(true);
                    Fire.transform.GetChild(0).localScale = new Vector3(energy / 10, energy / 10, 1);
                    Fire.transform.GetChild(1).gameObject.SetActive(false);
                    Fire.transform.GetChild(2).gameObject.SetActive(false);
                    Fire.transform.GetChild(3).gameObject.SetActive(false);
                }
                else if(energy<30)
                {
                    Fire.transform.GetChild(0).gameObject.SetActive(true);
                    Fire.transform.GetChild(1).gameObject.SetActive(true);
                    Fire.transform.GetChild(2).gameObject.SetActive(true);
                    Fire.transform.GetChild(0).localScale = new Vector3(1, 1, 1);
                    Fire.transform.GetChild(1).localScale = new Vector3(energy / 40, energy / 40, 1);
                    Fire.transform.GetChild(2).localScale = new Vector3(energy / 40, energy / 40, 1);
                    Fire.transform.GetChild(3).gameObject.SetActive(false);
                }else if (energy < 100)
                {
                    Fire.transform.GetChild(0).gameObject.SetActive(true);
                    Fire.transform.GetChild(1).gameObject.SetActive(true);
                    Fire.transform.GetChild(2).gameObject.SetActive(true);
                    Fire.transform.GetChild(3).gameObject.SetActive(true);
                    Fire.transform.GetChild(0).localScale = new Vector3(1, 1, 1);
                    Fire.transform.GetChild(1).localScale = new Vector3(0.5f, 0.5f, 1);
                    Fire.transform.GetChild(2).localScale = new Vector3(0.5f, 0.5f, 1);
                    Fire.transform.GetChild(3).localScale = new Vector3(( energy - 40 ) / 200, ( energy - 40 ) / 200, 1);
                }
                else
                {
                    Fire.transform.GetChild(0).gameObject.SetActive(true);
                    Fire.transform.GetChild(1).gameObject.SetActive(true);
                    Fire.transform.GetChild(2).gameObject.SetActive(true);
                    Fire.transform.GetChild(3).gameObject.SetActive(true);
                    energy = 100;
                }
            }
            cf2d.force = new Vector2(-transform.localPosition.x - 800, 0);
            if (state != State.Jump)
            {
                if (Input.GetKey(KeyCode.W))
                {
                    PressW();
                }
            }
            else
            {
                if (JumpCheck)
                {
                    Jumping = false;
                    //state = State.Hit;
                }
            }
            if (Input.GetKey(KeyCode.A))
            {
                PressA();
            }
            if (Input.GetKey(KeyCode.D))
            {
                PressD();
            }
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                if (Armas.ArmaShot())
                {
                    var arma = GameObject.Instantiate((GameObject)Resources.Load("Prefab/HitArma"), GameObject.Find("Eye0").transform) as GameObject;
                    arma.transform.position = GameObject.Find("Eye1").transform.position;
                    arma.transform.SetParent(GameObject.Find("Hit").transform, true);
                }
                else
                {
                    if (!reloadflag)
                    {
                        Invoke("Reload", 4);
                        reloadflag = true;
                    }
                }
            }
            if (transform.localPosition.y < -1000)
            {

                SceneManager.LoadScene(1);
            }

            Camera.main.gameObject.GetComponent<AudioSource>().volume=energy/60+0.4f;
        }

        void Reload()
        {
            Armas.Reload = true;
            reloadflag = false;
        }

    }
}